namespace Localisation.CustomProvider
{
    using System.Globalization;
    using System.Web.UI.Design;

    public class CustomDesignTimeResourceWriter : IDesignTimeResourceWriter
    {
        #region Fields

        private string fileOrClassName;
        private string cultureName;

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="CustomDesignTimeResourceWriter"/> class.
        /// </summary>
        /// <param name="fileOrClassName">Name of the file or class.</param>
        public CustomDesignTimeResourceWriter(string fileOrClassName) : this(fileOrClassName, CultureInfo.CurrentUICulture) { }

        /// <summary>
        /// Initializes a new instance of the <see cref="CustomDesignTimeResourceWriter"/> class.
        /// </summary>
        /// <param name="fileOrClassName">Name of the file or class.</param>
        /// <param name="cultureInfo">The culture info.</param>
        public CustomDesignTimeResourceWriter(string fileOrClassName, CultureInfo cultureInfo)
        {
            this.fileOrClassName = fileOrClassName;
            cultureName = cultureInfo.Name;
        }

        #endregion

        #region IDesignTimeResourceWriter Members

        /// <summary>
        /// Creates a key, using the provided string, to use to retrieve data from the given resource.
        /// </summary>
        /// <param name="resourceName">The name of the resource.</param>
        /// <param name="obj">The object to localize.</param>
        /// <returns>
        /// The key used to write or retrieve obj from resourceName.
        /// </returns>
        public string CreateResourceKey(string resourceName, object obj)
        {
            return obj.GetType().ToString(); // WTF?
        }

        #endregion

        #region IResourceWriter Members

        /// <summary>
        /// Adds an 8-bit unsigned integer array as a named resource to the list of resources to be written.
        /// </summary>
        /// <param name="name">Name of a resource.</param>
        /// <param name="value">Value of a resource as an 8-bit unsigned integer array.</param>
        /// <exception cref="T:System.ArgumentNullException">The name parameter is null. </exception>
        public void AddResource(string name, byte[] value)
        {
            AddResource(name, value.ToString());
        }

        /// <summary>
        /// Adds a named resource of type <see cref="T:System.Object"></see> to the list of resources to be written.
        /// </summary>
        /// <param name="name">The name of the resource.</param>
        /// <param name="value">The value of the resource.</param>
        /// <exception cref="T:System.ArgumentNullException">The name parameter is null. </exception>
        public void AddResource(string name, object value)
        {
            AddResource(name, value.ToString());
        }

        /// <summary>
        /// Adds a named resource of type <see cref="T:System.String"></see> to the list of resources to be written.
        /// </summary>
        /// <param name="name">The name of the resource.</param>
        /// <param name="value">The value of the resource.</param>
        /// <exception cref="T:System.ArgumentException">The name parameter is null. </exception>
        public void AddResource(string name, string value)
        {
            SqlResourceHelper.AddResource(name, fileOrClassName, cultureName, value);
        }

        /// <summary>
        /// Closes the underlying resource file or stream, ensuring all the data has been written to the file.
        /// </summary>
        public void Close() { }

        /// <summary>
        /// Writes all the resources added by the <see cref="M:System.Resources.IResourceWriter.AddResource(System.String,System.String)"></see> method to the output file or stream.
        /// </summary>
        public void Generate() { }

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose() { }

        #endregion
    }
}
